<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row v-show="showSearch">
				<el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
					<el-form-item label="设备编码" prop="equipmentCodeLike">
						<el-input placeholder="请输入设备编码" v-model="state.queryForm.equipmentCodeLike" />
					</el-form-item>
					<el-form-item label="所属企业" prop="airSiteNameLike">
						<el-input placeholder="请输入所属企业" v-model="state.queryForm.airSiteNameLike" />
					</el-form-item>
					<el-form-item>
						<el-button icon="search" type="primary" @click="getDataList"> 查询 </el-button>
						<el-button icon="Refresh" @click="resetQuery">重置</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<right-toolbar v-model:showSearch="showSearch" class="ml10 mr20" style="float: right"></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
			>
				<el-table-column prop="equipmentCode" label="设备编码" show-overflow-tooltip />
				<el-table-column prop="name" label="设备名称" show-overflow-tooltip />
				<el-table-column prop="equipmentTypeName" label="设备类型" show-overflow-tooltip />
				<el-table-column prop="status" label="设备状态" width="90">
					<template #default="{ row }">
						<el-tag :type="row.status === 2 ? 'success' : 'danger'">{{ row.status === 2 ? '在线' : '离线' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="airSiteName" label="所属企业" show-overflow-tooltip />
				<el-table-column prop="address" label="安装地点" show-overflow-tooltip width="200" />
				<!-- <el-table-column label="继电器控制" width="150">
					<template #default="{ row }">
						<el-button size="small" type="success" @click="relaySwitchDialogRef.openDialog(row.id)">开启</el-button>
						<el-button size="small" type="danger" @click="relaySwitchDialogRef.openDialog(row.id)">关闭</el-button>
					</template>
				</el-table-column> -->
				<el-table-column label="远程校准" width="100">
					<template #default="{ row }">
						<el-button
							size="small"
							type="primary"
							:disabled="row.status !== 2"
							@click="remoteCalibrationInfoRef.openDialog(row.equipmentCode, row.dataTemplateId, row.name)"
							>远程校准</el-button
						>
					</template>
				</el-table-column>
				<!-- <el-table-column label="远程配置" width="280">
					<template #default="{ row }">
						<el-button size="small">进入配置</el-button>
						<el-button size="small" plain type="primary" @click="commondDialogRef.openDialog(row.id)">AT命令配置</el-button>
						<el-button size="small" plain type="danger" @click="handleRestart">设备重启</el-button>
					</template>
				</el-table-column> -->
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>
		<!-- AT命令配置 -->
		<commond-dialog ref="commondDialogRef" @refresh="getDataList(false)" />
		<!-- 继电器开关 -->
		<relay-switch-dialog ref="relaySwitchDialogRef" @refresh="getDataList(false)" />
		<!-- 远程校准 -->
		<RemoteCalibrationInfo ref="remoteCalibrationInfoRef" @refresh="getDataList(false)" />
	</div>
</template>

<script setup lang="ts" name="airMonitorEquipmentControl">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, config } from '/@/api/platform/airMonitorEquipment';
import { useMessage, useMessageBox } from '/@/hooks/message';

// 引入组件
const CommondDialog = defineAsyncComponent(() => import('./components/commondDialog.vue'));
const RelaySwitchDialog = defineAsyncComponent(() => import('./components/relaySwitchDialog.vue'));
const RemoteCalibrationInfo = defineAsyncComponent(() => import('./components/remoteCalibrationInfo.vue'));
// 定义查询字典

// 定义变量内容
const commondDialogRef = ref();
const relaySwitchDialogRef = ref();
const remoteCalibrationInfoRef = ref();
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
	// createdIsNeed: false,
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	getDataList();
};

// 重启
const handleRestart = async () => {
	try {
		await useMessageBox().confirm('确定要重启该设备吗？');
	} catch {
		return;
	}

	try {
		await config();
		getDataList(false);
		useMessage().success('重启成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
</script>
